package com.fare.taluo.vo.core.report;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.hadoop.hbase.util.Bytes;

public class ReportVO {
	
	
	private Set<String> keySet=new HashSet<String>();

	@Override
	public boolean equals(Object obj) {
		if(!(obj instanceof ReportVO)){
			return false;
		}
		ReportVO other = (ReportVO)obj;
		if(keySet.size() != other.getKeySet().size()){
			return false;
		}
		Set<String> otherKeys = other.getKeySet();
		for(String key :keySet){
			if(!otherKeys.contains(key)){
				return false;
			}
		}
		return true;
	}


	
	
	public Set<String> getKeySet() {
		return keySet;
	}




	public void setKeySet(Set<String> keySet) {
		this.keySet = keySet;
	}




	@Override
	public int hashCode() {
		return normalize().hashCode();
	}




	@Override
	public String toString() {
		return normalize();
	}
	

	public byte[] rowkey(){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		DecimalFormat df = new DecimalFormat("00000");
		return Bytes.toBytes(normalize().hashCode()+200000000000l+sdf.format(new Date())+df.format(Math.random()*100000));
	}


	private String normalize() {
		List<String> list = new ArrayList<String>();
		list.addAll(keySet);
		Collections.sort(list);
		StringBuffer sb = new StringBuffer();
		for(String s:list){
			sb.append(s).append("#");
		}
		return sb.toString();
	}
		
	
	public static void main(String[] args){
		ReportVO vo = new ReportVO();
		vo.getKeySet().add("上海");
		vo.getKeySet().add("北京");
		vo.getKeySet().add("2013-01-14");
		vo.getKeySet().add("南方航空");
		
		ReportVO vo2 = new ReportVO();
		vo2.getKeySet().add("aaa1");
		vo2.getKeySet().add("dd2d");
		vo2.getKeySet().add("bbb");
		System.out.println(vo.equals(vo2));
		System.out.println(vo.hashCode());
		System.out.println(vo2.hashCode());
	}

}
